import { Msg, MsgLoading, PMsgLoadingClean } from '@C/Msg'
import { copyToClipboard } from '@U/common'

export default (src: string, fileSuffix: string) => {
    MsgLoading()
    uni.downloadFile({
        url: src,
        success: (res) => {
            if (res.statusCode === 200) {
                PMsgLoadingClean()
                const filePath = res.tempFilePath
                if (fileSuffix === 'zip') {
                    return copyToClipboard({ content: src, successTip: '链接复制成功，请在浏览器中打开', failTip: '链接复制失败，请重试' })
                }
                uni.openDocument({
                    filePath,
                    fail: () => { Msg({ icon: 'error', msg: '预览失败' }) },
                })
            } else { PMsgLoadingClean({ icon: 'error', msg: '下载失败' }) }
        },
        fail: () => {
            PMsgLoadingClean({ icon: 'error', msg: '下载失败' })
        },
    })
}
